package com.xinghen.ace.common.biz;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinghen.ace.common.model.Query;
import com.xinghen.ace.common.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

/**
 * @author xinghen
 * @ClassName: BaseBiz
 * @Package: com.xinghen.ace.common.biz
 * @Description 业务类基类
 * @create 2018-05-04 11:07
 */
public abstract class BaseBiz<M extends Mapper<T>, T> {

    @Autowired
    protected M mapper;

    /**
     * 获取单条数据
     *
     * @param entity
     * @return
     */
    public T selectOne(T entity) {
        return mapper.selectOne(entity);
    }

    /**
     * 通过ID获取数据
     *
     * @param id
     * @return
     */
    public T selectById(Object id) {
        return mapper.selectByPrimaryKey(id);
    }

    /**
     * 获取数据集合
     *
     * @param entity
     * @return
     */
    public List<T> selectList(T entity) {
        return mapper.select(entity);
    }

    /**
     * 获取所有数据
     *
     * @return
     */
    public List<T> selectListAll() {
        return mapper.selectAll();
    }

    /**
     * 获取数据条数
     *
     * @param entity
     * @return
     */
    public Integer selectCount(T entity) {
        return mapper.selectCount(entity);
    }

    /**
     * 条件查询数据集合
     *
     * @param example
     * @return
     */
    public List<T> selectByExample(Object example) {
        return mapper.selectByExample(example);
    }

    /**
     * 条件查询数据条数
     *
     * @param example
     * @return
     */
    public int selectCountByExample(Object example) {
        return mapper.selectCountByExample(example);
    }

    /**
     * 分页查询
     *
     * @param query
     * @return
     */
    @SuppressWarnings("unchecked")
    public PageInfo<T> selectByQuery(Query query) {
        Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
        Example example = new Example(clazz);
        if (query.entrySet().size() > 0) {
            Example.Criteria criteria = example.createCriteria();
            for (Map.Entry<String, Object> entry : query.entrySet()) {
                criteria.andLike(entry.getKey(), "%" + entry.getValue().toString() + "%");
            }
        }
        PageHelper.startPage(query.getPage(), query.getLimit());
        List<T> list = mapper.selectByExample(example);
        return new PageInfo<>(list);
    }

    /**
     * 插入数据
     *
     * @param entity
     */
    public void insert(T entity) {
        EntityUtils.setCreatAndUpdatInfo(entity);
        mapper.insert(entity);
    }

    /**
     * 插入数据
     *
     * @param entity
     */
    public void insertSelective(T entity) {
        EntityUtils.setCreatAndUpdatInfo(entity);
        mapper.insertSelective(entity);
    }

    /**
     * 删除数据
     *
     * @param entity
     */
    public void delete(T entity) {
        mapper.delete(entity);
    }

    /**
     * 删除数据
     *
     * @param id
     */
    public void deleteById(Object id) {
        mapper.deleteByPrimaryKey(id);
    }

    /**
     * 更新数据
     *
     * @param entity
     */
    public void updateById(T entity) {
        EntityUtils.setUpdatedInfo(entity);
        mapper.updateByPrimaryKey(entity);
    }

    /**
     * 更新数据
     *
     * @param entity
     */
    public void updateSelectiveById(T entity) {
        EntityUtils.setUpdatedInfo(entity);
        mapper.updateByPrimaryKeySelective(entity);
    }

}
